<?php
class cls_data_order extends cls_data_base
{
	var $table_name = '`orders`';
	var $table_name2 = '`orderitem`';
	var $db_name = 'b2c';
	
	function get_product_info($product_list)
	{
		$product_str = implode(',', $product_list);
		$sql = "select *
				from product p 
					left join productstatistic ps on ps.ProductID = p.ID
				where p.ID in ($product_str)
			";
		$info_list = $this->db->getAll($sql);
		$ret = array();
		foreach ($info_list as $info) {
			$ret[$info['ID']] = $info;
		}
		return $ret;
	}
	
	function place_order($order, $product_list)
	{
		$this->db->insert($this->table_name, $order);
		$order_id = $this->db->insert_id();
		$product_data = parent::get_instance('product');
		foreach ($product_list as $product) {
			$product['OrderID'] = $order_id;
			$this->db->insert($this->table_name2, $product);
			$product_data->reduce_quantity($product['ProductID'], $product['Amount']);
		}
	}
	
	function countRec($fname,$where) {
		$sql = "SELECT $fname FROM {$this->table_name}  $where";
		$result = $this->db->getAll($sql);
		return count($result);
	}
	
	
	function get_order_list($page,$rp,$where,$sort)
	{
		if (!$page) $page = 10;
		if (!$rp) $rp = 10;
		
		$start = (($page-1) * $rp);
		
		$limit = "LIMIT $start, $rp";
		
		$sql = "select * from {$this->table_name}  $where $sort $limit";
		$order_list = $this->db->getAll($sql);
		
		
		
		
			
		
		return $order_list;
		
	}
	
	
	function get_orderItem($pid)
	{
		$sql = "select * from OrderItem where OrderId = {$pid}";
		$orderItem_list = $this->db->getAll($sql);
		
		return $orderItem_list;
		
	}
 
    function modify_order($items)
    {
    	$item = explode(',', $items,2);
    	$orderstate  = null;
    //	p($items[2]);
    	switch ((int)$items[2])
    	{
    		case 1:
    			$orderstate = "未发货";
    			break;
    		case 2:
    			$orderstate = "已发货";
    			break;
    		case 3:
    			$orderstate = "已收货";
    			break;
    		case 4:
    			$orderstate = "取消";
    			break;
    		default:
    			$orderstate = "未发货";
    	}
    	
    	
    	$sql = "update {$this->table_name} set OrderState='{$orderstate}' where id={$item[0]}";
    //	p($sql);
    	$this->db->query($sql);
    	
    }
    
    function get_Address_info($address_id)
    {
    	$sql = "select * from address where ID = {$address_id}";
    	$address_info = $this->db->getRow($sql);
    	return $address_info;
    }
}